package br.com.etecmatao.admin.controller;

import br.com.etecmatao.business.BusinessLocator;
import br.com.etecmatao.business.impl.BusinessLocatorImpl;
import br.com.etecmatao.model.Noticias;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.imageio.ImageIO;

@ManagedBean
public class FormNoticia {

    private Noticias noticias = new Noticias();
    private BusinessLocator businessLocator;
    private DataModel<Noticias> lista;

    public DataModel<Noticias> getLista() {
        lista = new ListDataModel<Noticias>(businessLocator.getNoticiasBusiness().finByAll());
        return lista;
    }

    public void setLista(DataModel<Noticias> lista) {
        this.lista = lista;
    }

    public FormNoticia() {
        noticias = new Noticias();
        businessLocator = new BusinessLocatorImpl();
    }

    public Noticias getNoticias() {
        return noticias;
    }

    public void setNoticias(Noticias noticias) {
        this.noticias = noticias;
    }

    public void save() {
        noticias.setLogin((br.com.etecmatao.model.Login) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user"));
        noticias.setData(new Date());
        noticias.setStatus("A");
        
        if (noticias.getIdNoticias() != null) {
            businessLocator.getNoticiasBusiness().update(noticias);
        } else {
            businessLocator.getNoticiasBusiness().save(noticias);
        }
        FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage("Cadastro realizado com sucesso"));
    }

    public void delete(){
        noticias = lista.getRowData();
        businessLocator.getNoticiasBusiness().delete(noticias);
    }

    public String editar(){
        noticias = lista.getRowData();
        return "formNoticias";
    }

    public String create(){
        noticias = new Noticias();
        return "formNoticias";
    }
}